<?
include_once("../functions/functions.php");

//FUNCTION DECLARATIONS

function getReturnDate($VisitID)
{
	$queryReturnDates = "SELECT return_date
						FROM er_visit
						WHERE visit_id = '$VisitID'
						ORDER BY return_date";
	$resultReturnDates = r_db($queryReturnDates);
	echo "<td valign=\"top\">";
		while($returnDates = mysql_fetch_array($resultReturnDates))
		{
			echo "$returnDates[0] <br />";
		}
	echo "</td>";

}


function getNoOfVisits1($ClientID)
{
	$queryNoOfVisits = "SELECT er_Client.client_id, COUNT(er_visit.visit_id)
						FROM er_Client, er_visit
						WHERE er_Client.client_id=er_visit.client_id
						AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						AND er_Client.client_id='$ClientID'
						GROUP BY er_Client.client_id";
	$resultNoOfVisits = r_db($queryNoOfVisits);
	$rowNoOfVisits = mysql_fetch_array($resultNoOfVisits);
	$noOfVisits = $rowNoOfVisits[1];
	return $noOfVisits;
}
function getPaymentDetails1($ClientID, $rowPayment, $visitPaymentID)
{
	$queryVisit = "SELECT a.payment_amount
				FROM er_Payment_On_Visit as a INNER JOIN er_Visit as b ON b.visit_id = a.visit_id
				WHERE b.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
					AND a.payment_id=$rowPayment
					AND b.visit_id = $visitPaymentID
					ORDER BY b.visit_date, b.visit_id";
	$resultVisit = r_db($queryVisit);
	return $resultVisit;
}
function getTotalAmountOnVisit1($ClientID, $VisitID)
{
	$queryErGiven = "SELECT SUM(er_Payment_On_Visit.payment_amount) as pa, er_visit.visit_id
						FROM er_visit, er_Payment_On_Visit
						WHERE er_visit.visit_id=er_Payment_On_Visit.visit_id
						AND er_visit.visit_id = $VisitID
						AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
						GROUP BY er_visit.visit_date, er_visit.visit_id";
	$resultErGiven = r_db($queryErGiven);
	return $resultErGiven;
}

function getIncomeSources($VisitID)
{
	$query = "select er_income_source.income_type,
	er_client_income.income_amount,
	er_client_income.when_due,
	er_visit.visit_id
	FROM er_income_source,er_client_income,er_visit
	where er_client_income.income_id=er_income_source.income_id
	AND er_visit.visit_id=er_client_income.visit_id
	AND er_client_income.visit_id=$VisitID
	group by income_type";

	$results=r_db($query);
	echo "<td valign=\"top\">";
	while ($rowIncomeSource = mysql_fetch_array($results)) {

		$duedate =  $rowIncomeSource[2];
		$DbDOB=explode("-",$duedate);
		$yearDOB=$DbDOB[0];
		$monthDOB=$DbDOB[1];
		$dayDOB=$DbDOB[2];
		$duedate=$dayDOB."/".$monthDOB."/".$yearDOB;

		if($rowIncomeSource[0]<>"Select Client Income")
		{
			echo Bullet()."$rowIncomeSource[0] - $".number_format($rowIncomeSource[1],2);
			if ($duedate != '00/00/0000') {
				echo " - $duedate";
			}
			echo "<br />";
		}
	}
	echo "&nbsp;</td>";
}

function getEmergencyReliefReasons($VisitID)
{
	$query ="SELECT er_Emergency_Relief.er_reason, er_Relief_On_Visit.visit_id
			FROM er_Emergency_Relief,er_Relief_On_Visit
			WHERE	er_Relief_On_Visit.relief_id=er_Emergency_Relief.er_id
			AND er_Relief_On_Visit.visit_id = $VisitID
			ORDER BY er_Relief_On_Visit.visit_id";
	$results=r_db($query);
	echo "<td valign=\"top\">";
	while($rowErGiven = mysql_fetch_array($results))
	{
		echo Bullet()."$rowErGiven[0]<br>";
	}
	echo "</td>";
}

function Bullet () {
	echo "<span style=\"font-size: 10px; font-weight: bold; font-family: symbol;\">�</span> ";
}

function GetVisitDetails1($ClientID)
{
	$queryVisitDate = "SELECT er_Client.client_id,
							  er_visit.visit_id,
							  er_visit.visit_date,
							  er_site.site_name,
							  er_visit.interviewer,
		  					er_visit.action_taken,
							  er_visit.day_book_colour,
							  er_visit.day_book_page_no,
							  er_visit.no_children_assistted,
							  er_visit.no_adults_assistted,
							  er_visit.address,
							  er_housing_type.housing_type,
							  er_visit.phone_no,
							  er_visit.mobile_no,
							  er_income_status.income_status_type,
							  er_visit.other_housing_type,
							  er_visit.centrelink_loan_amt_borrowed,
							  er_visit.rent_paid_pf,
							  er_agency.agency_name,
							  er_visit.other_income_status,
							  er_visit.visit_recorded_date,
							  er_visit.suburb,
							  er_visit.state,
							  er_visit.postcode,
							  er_visit.er_given,
							  er_visit.er_reason,
							  er_visit.other_er,
							  er_visit.client_signature,
							  er_visit.er_amount_receipt_no,
							  er_visit.reason_cheque_payment,
							  er_visit.cheque_payee,
							  er_visit.other_payment_type,
							  er_visit.extra_food_parcels,
							  er_visit.other_financial,
							  er_visit.other_income_source
							  FROM er_Client, er_visit,er_housing_type,er_income_status,er_agency, er_site
							  WHERE er_Client.client_id=er_visit.client_id
							  AND er_visit.site_visited_id=er_site.site_id
							  AND er_visit.visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)
							  AND er_Client.client_id=$ClientID
							  AND er_visit.housing_type_id=er_housing_type.housing_type_id
							  AND er_visit.income_status_id=er_income_status.income_status_id
							  AND er_visit.agency_id=er_agency.agency_id
							  ORDER BY er_visit.visit_date, er_visit.visit_id";
	$resultVisit = r_db($queryVisitDate);
	return $resultVisit;
}

function getIDtype($idtype)
{
	$query="select identification_type from er_Identification_Type where identification_no='".$idtype."'";
	$resultIDtype = r_db($query);
	$row=mysql_fetch_array($resultIDtype);
	return $row;
}



function getActionTakenReasons($VisitID)
{
	$query ="SELECT er_action_taken.action_taken, er_action_on_visit.visit_id
			FROM er_action_taken,er_action_on_visit
			WHERE	er_action_on_visit.action_id=er_action_taken.action_id
			AND er_action_on_visit.visit_id = $VisitID
			ORDER BY er_action_on_visit.visit_id";
	$results=r_db($query);
	echo "<td valign=\"top\">";
	while($rowErGiven = mysql_fetch_array($results))
	{
		echo Bullet()."$rowErGiven[0]<br>";
	}
	echo "</td>";
}
?>



<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" type="text/css" href="../print.css">
<script LANGUAGE="JavaScript">
<!--
function windowPrint() {
	window.print();
	setTimeout("window.close();",1500);
}
 -->
</script>
</head>
<body onload="windowPrint();">

<?
global $ClientID;
if (!isset($ClientID)) {
	$ClientID = $_GET['ClientID'];
}
if ($ClientID > NULL) {
	$result = GetVisitDetails1($ClientID);
	$i = 0;
	$visit = array();

	//begin while
	while ($temp = mysql_fetch_array($result)) {
		$visit[$i] = $temp;
		$i++;
	}

	//create colspan variable
	$colspan = count($visit) + 1;
	if ($colspan < 1) {
		$colspan = 0;
	}

if (isset($ClientID))
{
	$query = "SELECT * FROM er_Client WHERE client_id ='$ClientID'";
	$sql = r_db($query);
	$row = mysql_fetch_array($sql);
	$idtyperesult=getIDtype($row[10]);
	echo "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\n";
	echo "<tr><td colspan=\"4\"><h3>Client Details</h3></td></tr>\n";
	echo  "<tr>
		<td width=\"20%\"><b>Surname:</b></td>
		<td width=\"30%\">$row[2]&nbsp;</td>
		<td width=\"20%\"><b>Given Name:</b></td>
		<td width=\"30%\">".$row[3]."&nbsp;</td>
	</tr>\n";

	$DOB =  $row[4];
	$DbDOB=explode("-",$DOB);
	$dayDOB=$DbDOB[2];
	$monthDOB=$DbDOB[1];
	$yearDOB=$DbDOB[0];
	$DOB=$dayDOB."/".$monthDOB."/".$yearDOB;

	if ($row[6]=="f")
		$gender="Female";
	else
		$gender="Male";
	echo  "<tr>
		<td><b>Date of Birth:</b></td>
		<td>$DOB</td>
		<td><b>Gender:</b></td>
		<td>$gender&nbsp;</td>
	</tr>\n";

	echo "<tr>
		<td><b>ID Type:</b></td>
		<td>".$idtyperesult[0]. "&nbsp;</td>
		<td><b>ID Reference No:</b></td>
		<td>" . $row[1]."&nbsp;</td>
	</tr>\n";
	$otheridvalue = $row[8];
	if ($otheridvalue != null)
	{
		echo   "<tr><td>&nbsp;</td><td>&nbsp;</td><td><b>Other ID Type:</b></td><td>" . $row[8]."&nbsp;</td></tr>\n";
	}


	echo "</table>";
}

$noOfVisits = getNoOfVisits1($ClientID);
if ($noOfVisits > 0) {
?>
<br />

<table border="1" cellpadding="0" cellspacing="0" width="100%">
	<tr><td><h3>Current Year Visit(s)</h3></td>
		<?
		foreach ($visit as $v => $value) {
			if ($v < 3) {
				echo "<td><b>Visit #". ($v+1) ."</b></td>";
			}
			elseif ($v < 4) {
				echo "<td><b>Visit #". ($v+1) ."</b></td>";
			}
			else {
				echo "<td><b>Visit #". ($v+1) ."</b></td>";
			}
		}
		?>
	</tr>
	<tr><td valign="top"><B>Date</B></td>
		<?
		foreach ($visit as $v => $value) {
			$DbDOB=explode("-",$value['visit_date']);
			@$dayDOB=$DbDOB[2];
			@$monthDOB=$DbDOB[1];
			$yearDOB=$DbDOB[0];
			$dobDate=$dayDOB."/".$monthDOB."/".$yearDOB;
			echo "<td>$dobDate</td>";
		}
		?>
	</tr>
	<tr><td><B>Site</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['site_name']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>Address</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['address']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>Suburb</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['suburb']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>State</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['state']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>Post Code</B>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['postcode']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>Phone Number</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['phone_no']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>Phone Number</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['mobile_no']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>No. of Children assisted</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['no_children_assistted']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>No. of Adults assisted</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['no_adults_assistted']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr>
		<td colspan="<? echo $colspan; ?>"	valign="bottom"><h3>Assistance Provided</h3></td>
	</tr>

<?
//retrieve visit details

//print visit number
/*
echo "<tr>";
echo "<td  width=\"25%\">&nbsp;</td>";
for($i=1;$i<=$noOfVisits;$i++)
{
	echo "<td>Visit $i</td>";
}
echo "</tr>";
*/
//end print visit number

$queryPayment = "SELECT * FROM er_payment";
$resultPayment = r_db($queryPayment);
for($i=1;$i<=$noOfVisits+1;$i++)
{
	while ($rowPayment = mysql_fetch_array($resultPayment))
	{
		echo "<tr><td valign=\"top\" width=\"25%\"><B>$rowPayment[1] ($)</B></td>";

		$sql = "SELECT visit_id from er_visit WHERE client_id = $ClientID AND visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR) ORDER BY visit_date";
		$result = r_db($sql);
		$visits = NULL;

		while ($visits = mysql_fetch_array($result)) {
			$arrVisitDetails = getPaymentDetails1($ClientID,$rowPayment['payment_id'], $visits['visit_id']);
			$rowVisit = mysql_fetch_array($arrVisitDetails);
			if ($rowVisit['payment_amount'] > 0) {
				{

					//Print the amount in the appropriate visit coloumn

					$width = 75/($noOfVisits+1);
					round($width,0);
					echo "<td valign=\"top\" align=\"left\">". number_format($rowVisit['payment_amount'],2);
					echo "</td>";

				}
			}
			else {
				$width = 75/($noOfVisits+1);
				round($width,0);
				echo "<td valign=\"top\" align=\"left\">&nbsp;</td>";
			}
		}
		echo "</tr>\n";
	}
}
echo "<tr><td><B>Total Amount Given ($)</B></td>";

$sql = "SELECT visit_id from er_visit WHERE client_id = $ClientID AND visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR) ORDER BY visit_date";
$result = r_db($sql);
$visits = NULL;

while ($visits = mysql_fetch_array($result))
{
	$resultPayment = getTotalAmountOnVisit1($ClientID, $visits['visit_id']);
	$rowErGiven = mysql_fetch_array($resultPayment);
	echo "<td align=\"left\"><B>";
	echo number_format($rowErGiven['pa'],2)  ;
	echo "</B>&nbsp;</td>";
}

echo "</tr>\n";
//finish visit details
?>
	<tr><td valign="top"><B>Other Payment Type: </B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['other_payment_type']."&nbsp;</td>";
		}
		?>
	</tr>

		<tr><td valign="top"><B>Extra Food Parcels: </B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['extra_food_parcels']."&nbsp;</td>";
		}
		?>
	</tr>

		<tr><td valign="top"><B>Cheque Number: </B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['er_amount_receipt_no']."&nbsp;</td>";
		}
		?>
	</tr>
		<tr><td valign="top"><B>Cheque Payee: </B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['cheque_payee']."&nbsp;</td>";
		}
		?>
	</tr>
		<tr><td valign="top"><B>Reason for cheque: </B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['reason_cheque_payment']."&nbsp;</td>";
		}
		?>
	</tr>

	<tr><td colspan="<? echo $colspan; ?>"><h3>Sources of Household Income</h3></td></tr>
	<tr><td valign="top"><B>Sources of Household Income($)</B></td>
		<?
		foreach ($visit as $v => $value) {
			getIncomeSources($value['visit_id']);
		}
		?>
	</tr>
	<tr><td valign="top"><B> Other Income Source</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['other_income_source']."&nbsp;</td>";
		}
		?>
	</tr>

	<tr><td colspan="<? echo $colspan; ?>"><h3>Income Status</h3></td></tr>
	<tr><td valign="top"><B>Income Status</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['income_status_type']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B> Other Income Status</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['other_income_status']."&nbsp;</td>";
		}
		?>
	</tr>

	<tr><td colspan="<? echo $colspan; ?>"><h3>Housing Type</h3></td></tr>
	<tr><td valign="top"><B>Housing Type</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['housing_type']."&nbsp;</td>";
		}
		?>
	</tr>
	<tr><td valign="top"><B>Other Housing Type</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['other_housing_type']."&nbsp;</td>";
		}
		?>
	</tr>

	<tr><td valign="top"><B>Rent Paid per fortnight ($)</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>$".number_format($value['rent_paid_pf'],2)."&nbsp;</td>";
		}
		?>
	</tr>

	<tr><td colspan="<? echo $colspan; ?>"><h3>Reason for Emergency Relief</h3></td></tr>
	<tr><td valign="top"><B>Reasons for seeking assistance</B></td>
		<?
		foreach ($visit as $v => $value) {
			getEmergencyReliefReasons($value['visit_id']);
		}
		?>
	</tr>
	<tr>
	<td valign="top"><B> Other Reason for ER</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['other_er']."&nbsp;</td>";
		}
		?>
	</tr>

	<tr><td valign="top"><B>Client Referred By</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['agency_name']."&nbsp;</td>";
		}
		?>
	</tr>

		<tr><th>Action Taken Associated with E.R.</th></tr>
		<tr><td valign="top">Action Taken</td>
			<?
			foreach ($visit as $v => $value) {
				getActionTakenReasons($value['visit_id']);
			}
			?>
		</tr>
		<tr><td valign="top">Return Date</td>
			<?
			foreach ($visit as $v => $value) {
				getReturnDate($value['visit_id']);
			}
			?>
		</tr>
		<tr>
		<td valign="top"> Other Action Taken (Financial Only)</td>
			<?
			foreach ($visit as $v => $value) {
				echo "<td>".$value['other_financial']."</td>";
			}
			?>
		</tr>

	<tr><td valign="top"><B>Interviewer</B></td>
		<?
		foreach ($visit as $v => $value) {
			echo "<td>".$value['interviewer']."&nbsp;</td>";
		}
		?>
	</tr>
</table>
<?
		}
		else {
			echo "<p>This client has no visits within the last year.</p>";
		}
	}
?>
<script language="javascript">
	window.print();
</script>
</body>
</html>